package com.heo.music.player.util;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.TagException;

import java.io.File;
import java.io.IOException;

/**
 * created on 2024-05-15 13:46
 *
 * @author 星天无极
 **/
public class MusicDurationCalculator {
    public static int getDuration(File file) {
        try {
            AudioFile audioFile = AudioFileIO.read(file);
            return audioFile.getAudioHeader().getTrackLength();
        } catch (CannotReadException | IOException | TagException | ReadOnlyFileException |
                 InvalidAudioFrameException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static void main(String[] args) {
        // Example usage
        int duration = getDuration(new File("data/music/南国的孩子-张悬.mp3"));
        System.out.println("Music duration: " + duration);
    }
}
